<?php
session_start();
/**
 * Created by PhpStorm.
 * User: Pico_Dragon
 * Date: 15/12/2014
 * Time: 10:53
 */
include "../../db/dbConnect.php";
include '../function/pagging.php';

error_reporting(E_ALL && ~ E_NOTICE);
$p=new Pager();
$p->setLimit(10);
$p->findStart($p->getLimit());

if($_SERVER['REQUEST_METHOD'] == 'GET') {
    $keyword = mysql_real_escape_string($_GET['keyword']);
    $permit=mysql_real_escape_string($_GET['permit']);
    $isOnline=mysql_real_escape_string($_GET['online']);
    $isActive=mysql_real_escape_string($_GET['active']);

    $where="where deleted=0 ";
    if($keyword !=''){
        $where.= "and (lstname LIKE N'%" . $keyword . "%'
            or fstName like N'%" . $keyword . "%'
            or email LIKE '%" . $keyword . "%')";
        $p->setKeySrch($keyword);
    }
    if($permit!=''){
        $where.=" and permit=".$permit." ";
        $p->setKindKey($permit);
    }
    if($isOnline !=''){
        $where.=" and online=".$isOnline." ";
        $p->setStatusKey($isOnline);
    }
    if($isActive!=''){
        $where.=" and active=".$isActive." ";
        $p->setActive($isActive);
    }


    $sql="select * from u_user ".$where;

}
$sql2=$sql;
$result=mysql_query($sql);
if(mysql_error()){
    echo mysql_error();
    exit();
}

$count=0;
$count=mysql_num_rows($result);
$p->findPages($count, $p->getLimit());

$start=$p->getStart();
$limit=$p->getLimit();
$sql2.=' limit '.$start.','.$limit.'';

$result=mysql_query($sql2);
if(mysql_error()){
    echo mysql_error();
    exit();
}
?>

<table class="listQ w100p">
    <thead>
    <th class="tl w3p"><input type="checkbox" name="all" id="all" chosen="0" title="Chọn tất cả"></th>
    <th class="tc w3p ">STT</th>
    <th class="tc w30p">Tên tài khoản</th>
    <th class="tc" style="width: 12%">Phân quyền</th>
    <th class="tc w15p">Ngày tham gia</th>
    <th class="tc w10p">Trạng thái</th>
    <th class="tc w10p">Hoạt động</th>
    <th class="tc w15p">Xử lý</th>
    </thead>
    <div class="clear"></div>
    <tbody>
    <?php
    $i=0;
    while($row=mysql_fetch_array($result)) {
        $i++;
        $oe=(($i % 2)==1)?'o':'e';
        ?>
        <tr class="<?php echo $oe;?>">
            <td class="tl"><input type="checkbox" class="one" name="one" id="<?php echo $row['iduser']?>" title="Chọn"></td>
            <td class="tc" style="border-left: 1px solid #ccc;"><?php echo $i;?></td>
            <td class="tl" style="border-left: 1px solid #ccc;"><?php echo $row['lstName'].' '.$row['fstName'];?>
                <?php if($row['email']){?><div><a href="mailto:<?php echo $row['email']?>"><?php echo $row['email']?></a></div><?php }?>
            </td>
            <td class="tc" style="border-left: 1px solid #ccc;"><?php echo ($row["permit"])?"<span class='onl'>Quản trị</span>":"Thành viên";?></td>
            <td class="tc" style="border-left: 1px solid #ccc;"><?php echo date("d/m/Y H:i a",strtotime($row['dateCreated'])); ?></td>
            <td class="tc" style="border-left: 1px solid #ccc;"><?php echo ($row["online"])?"<span class='onl'>Trực tuyến</span>":"<span class='off'>Vắng</span>";?></td>
            <td class="tc" style="border-left: 1px solid #ccc;"><?php
                $active=false;
                if($row['active']==2){
                    echo "Chưa kích hoạt";
                }elseif($row['active']==1){
                    echo "Hoạt động";
                    $active=1;
                }elseif($row['active']==0){
                    echo "Đang khóa";
                }else echo '-';
                ?>
            </td>

            <td class="tr"  style="border-left: 1px solid #ccc;">
                <button id="del" class="btn btn-small left del" idu="<?php echo $row['iduser'];?>">Xóa</button>
                <button id="active" class="btn btn-small activeBtn" idu="<?php echo $row['iduser']; ?>">
                    <?php if($active){echo "Khóa";}else echo "Kích hoạt";?></button>
            </td>
        </tr>

        <div class="clear"></div>
        <?php
        unset($active);
    }
    if($i==0){
        echo '<tr>
                        <td colspan="8">Không có bản ghi nào!</td>
                    </tr>';
    }
    ?>
    <tr>
        <td colspan="8">
            <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#E0FFFF">
                <tr>
                    <td align="center" valign="middle">Từ <?php echo($p->getStart()+1).' đến '.((($end=($p->getStart()+$p->getLimit()))>$count)?$count:$end).' trong tổng cộng '.$count.'';?></td>
                    <td align="center" valign="middle">
                        <?php
                        $p->setLink();
                        $page=$p->pageList($_GET["page"],$p->getPage(),"web/admin/userSearch.php");
                        echo $page;
                        ?></td>
                </tr>
            </table>
        </td>
    </tr>
    </tbody>
</table>

<script type="text/javascript">
    $(".del").bind('click', function(e){
        if(confirm("Bạn có chắc chắn  muốn xóa tài khoản này?")){
            e.preventDefault();
            var idu = $(this).attr('idu'), page=$("#pag_nav").text();
            $.ajax({
                url:"web/admin/userDel.php",
                type:"POST",
                data: 'idu='+idu,
                success:function(re){
                    $("#tbList").load("web/admin/userSearch.php?page="+page).fadeIn('slow');
                    $(".result").hide().text(re).slideDown('slow').fadeOut(4000);
                }
            })
        }
    });
    $('.activeBtn').bind('click',function(e){
        e.preventDefault();
        $(this).attr('disable',true);
        var idu=$(this).attr('idu'), page=$("#pag_nav").text();
//        $.post("web/admin/userSwitch.php",{idu: idu})
        $.ajax({
            url:"web/admin/userSwitch.php",
            type: "post",
            data: 'idu='+idu,
            success:function(re){
                $("#tbList").load("web/admin/userSearch.php?page="+page).fadeIn('slow');
                $(".result").hide().text(re+'!').slideDown('fast').fadeOut(4000);
            }
        })
//        $(".saContent").load("web/admin/userList.php").fadeIn('slow');
    });

    $("#multi-del").bind('click', function(e){
        e.preventDefault();
        var checked=false;
        var idu, i=0, page=$("#pag_nav").text();
        if(!$(this).attr('num') && !$(this).attr('full') ){
            alert("Hãy chọn ít nhất một bản ghi!");
        }else{
            $(".one.checked").each(function(){
                i++;
                idu=$(this).attr('id');
                $.ajax({
                    url:"web/admin/userDel.php",
                    type:"POST",
                    data:'idu='+idu,
                    success:function(re){
                        $("#tbList").load("web/admin/userSearch.php?page="+page).fadeIn('slow');
                        $curr.attr('num','0');
                        $curr.attr('full','0');
                    }
                })
            })
            if(i)$(".result").hide().text(re+' '+i+' bản ghi!').slideDown('slow').fadeOut(4000);
        }

    });

    $("#multi-active").bind('click', function(e){
        e.preventDefault();
        var checked=false, $curr=$(this);
        var idu, i=0;
        // alert("full:"+$(this).attr('full')+' num:'+$(this).attr('num'));
        if(!$(this).attr('num') && !$(this).attr('full')){
            alert("Hãy chọn ít nhất một bản ghi!");
        }else{
            $(".one.checked").each(function(){
                var $ck = $(this);
                i++;
                idu=$ck.attr('id');
                $.ajax({
                    url:"web/admin/userSwitch.php",
                    type:"POST",
                    data:'idu='+idu,
                    success:function(re){
                        $("#tbList").load("web/admin/userSearch.php?page="+page).fadeIn('slow');
                        $curr.attr('num','0');
                        $curr.attr('full','0');
                    }
                })

            })
            if(i)$(".result").hide().text(re+' '+i+' bản ghi!').slideDown('slow').fadeOut(4000);
        }

    });


    $("#all").click(function(){
        var checked_status = this.checked;
        $(".one").each(function(){
            this.checked = checked_status;
            if($(this).is(':checked')){
                $("#multi-del").attr('full','1');//danh dau tren nut xoa nhieu la da chon het
                $("#multi-active").attr('full','1');//danh dau tren nut xoa nhieu la da chon het
                $(this).addClass('checked').removeClass('unchecked');
            }else{
                $("#multi-del").attr('full','0');//danh dau tren nut xoa nhieu la chua chon ban ghi nao
                $("#multi-del").attr('num',0);
                $("#multi-active").attr('full','0');//danh dau tren nut xoa nhieu la chua chon ban ghi nao
                $("#multi-active").attr('num',0);
                $(this).addClass('unchecked').removeClass('checked');
            }
        });
    });
    $(".one").click(function(){
        if($(this).is(':checked')){
            var num=$("#multi-del").attr('num');
            num++;
            $("#multi-del").attr('num',num);
            $("#multi-active").attr('num',num);
            $(this).addClass('checked').removeClass('unchecked');
        }else{
            $(this).addClass('unchecked').removeClass('checked');
            var num=$("#multi-del").attr('num');
            num--;
            $("#multi-del").attr('num',num);
            $("#multi-active").attr('num',num);
        }

        if($('input[name=all]').is(':checked')){

            $('input[name=all]').prop('checked', false);
            $("#multi-del").attr('full','0');
            $("#multi-active").attr('full','0');
        }
    });


    $('a.pag_nav').click(function(){
        var $href=$(this).attr('href');
        //alert($href);
        $(" #tbList").load($href);
        return false;
    });
</script>